package tms.presentation.bean.login;

import tms.business.login.LoginService;
import tms.util.activity.UserActivityManager;
import tms.util.constants.NavigationConstant;
import tms.util.constants.RequestParameter;
import tms.util.constants.UiMessage;
import tms.util.exception.ApplicationRuntimeException;
import tms.util.jsf.FacesContextManager;
import tms.util.localization.LocalizationManager;
import tms.util.security.AppSecurityManager;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

@Named
@ConversationScoped
public class LoginViewBean implements Serializable {
    @Inject
    FacesContextManager facesContextManager;
    @Inject
    LocalizationManager localizationManager;
    @Inject
    AppSecurityManager securityManager;
    @Inject
    UserActivityManager userActivityManager;
    @EJB
    LoginService loginService;

    private String userName;
    private String password;
    private String passwordConfirm;
    private boolean rememberMe;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public boolean getRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }
    

    public String createNewUser() {
        String retVal = null;
        if (password != null && password.length() != 0 && password.equals(passwordConfirm)) {
            try {
                loginService.createUser(userName, password);
                facesContextManager.addMessage(localizationManager.localizedMessage(UiMessage.NEW_USER_REGISTERED));
                retVal = NavigationConstant.SUCCESS.name();
            } catch (ApplicationRuntimeException e) {
                facesContextManager.addMessage(localizationManager.localizedMessage(e.getUiMessage()));
            }
        } else {
            facesContextManager.addMessage(localizationManager.localizedMessage(UiMessage.INCORRECT_PASSWORD_CONFIRM));
        }
        return retVal;
    }

    public String login() {
        String retVal = null;
        try {
            securityManager.login(userName, password, rememberMe);
            retVal = NavigationConstant.SUCCESS.name();
        } catch (ApplicationRuntimeException e) {
            facesContextManager.addMessage(localizationManager.localizedMessage(e.getUiMessage()));
        }
        return retVal;
    }

    public String logout() {
        securityManager.logout();
        ((HttpServletRequest)facesContextManager.getFacesContext().getExternalContext().getRequest()).getSession().invalidate();
        return NavigationConstant.LOGOUT.name();
    }

    public void checkAccess(ComponentSystemEvent event) throws AbortProcessingException {
        if (!securityManager.isLoggedIn()) {
            facesContextManager.addRedirectSurviveMessage(localizationManager.localizedMessage(UiMessage.AUTHENTICATION_REQUIRED));
            facesContextManager.navigateForOutcome(NavigationConstant.LOGIN_REQUIRED);
        }
    }

    public void checkRemembered(ComponentSystemEvent event) throws AbortProcessingException {
        if (securityManager.checkRemembered()) {
            facesContextManager.getRedirectSurviveMessages(); // cleanup all previously added messages
            facesContextManager.navigateForOutcome(NavigationConstant.REMEMBERED_USER);
        }
    }

    public void registerActivity() {
        boolean isTimeRefresherActivity = "true".equals(facesContextManager.getRequestParameter(RequestParameter.TIME_REFRESHER_REQ));
        if (securityManager.isLoggedIn() && !isTimeRefresherActivity) {
            userActivityManager.userActive(securityManager.getUser().getId());
        }
    }
}
